<template>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <el-header class="header">
      <div class="header-content">
        <!-- 侧边栏切换按钮 -->
        <el-button
            class="sidebar-toggle"
            type="text"
            @click="toggleSidebar"
        >
          <i :class="showSidebar ? 'el-icon-s-fold' : 'el-icon-s-unfold'"></i>
        </el-button>

        <div class="logo">
          <i class="el-icon-s-home mr-2"></i>
          <span>租房信息分析系统</span>
        </div>
      </div>

      <!-- 用户信息区域 -->
      <div class="user-info">
        <el-dropdown trigger="hover" @command="handleCommand">
          <span class="el-dropdown-link">
            <el-avatar size="small" icon="el-icon-user"></el-avatar>
            <span class="ml-2">{{ username }}</span>
            <i class="el-icon-arrow-down el-icon--right ml-1"></i>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item command="profile">
                <i class="el-icon-user-solid mr-2"></i>个人中心
              </el-dropdown-item>
              <el-dropdown-item command="logout">
                <i class="el-icon-switch-button mr-2"></i>退出登录
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </el-header>

    <!-- 主体内容区 -->
    <el-container>
      <!-- 侧边栏 - 添加过渡动画 -->
      <el-aside
          class="sidebar"
          :width="showSidebar ? '200px' : '0px'"
          transition="sidebar-transition"
      >
        <el-menu
            :default-active="activeMenu"
            class="el-menu-vertical-demo"
            router
            background-color="#2f3640"
            text-color="#dcdfe6"
            active-text-color="#409eff"
        >
          <!-- 菜单内容保持不变 -->
          <el-menu-item index="/user/first-index">
            <i class="el-icon-s-finance"></i>
            <span>首页展示</span>
          </el-menu-item>

          <el-submenu index="sub1">
            <template #title>
              <i class="el-icon-s-data"></i>
              <span>功能菜单</span>
            </template>
            <el-menu-item index="/user/house-search">
              <i class="el-icon-s-finance"></i>
              <span>房源查询</span>
            </el-menu-item>
            <el-menu-item index="/user/quarter-analysis">
              <i class="el-icon-s-finance"></i>
              <span>季度分析</span>
            </el-menu-item>
            <el-menu-item index="/user/AreaRanking">
              <i class="el-icon-s-finance"></i>
              <span>区域排名</span>
            </el-menu-item>
            <!-- 价格排名下拉菜单 -->
            <el-submenu index="price-ranking">
              <template #title>
                <i class="el-icon-s-claim"></i>
                <span>价格排名</span>
              </template>
              <el-menu-item index="/user/price/ranking1">
                <span>房源价格排名</span>
              </el-menu-item>
              <el-menu-item index="/user/price/ranking2">
                <span>区域价格排名</span>
              </el-menu-item>
              <el-menu-item index="/user/price/ranking3">
                <span>月份价格排名</span>
              </el-menu-item>
            </el-submenu>

<!--            <el-menu-item index="/user/history">-->
<!--              <i class="el-icon-s-claim"></i>-->
<!--              <span>历史记录</span>-->
<!--            </el-menu-item>-->

            <!-- 仅管理员可见的用户管理菜单 -->
            <el-menu-item
                v-if="isAdmin"
                index="/user/user-management"
            >
              <i class="el-icon-s-management"></i>
              <span>用户管理</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>

      <!-- 右侧内容区 - 动态调整边距 -->
      <el-main
          class="main-content"
          :style="{ paddingLeft: showSidebar ? '20px' : '0px' }"
      >
        <transition name="fade">
          <router-view />
        </transition>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "AppLayout",
  data() {
    return {
      username: '管理员',
      showSidebar: true // 控制侧边栏显示状态
    };
  },
  computed: {
    activeMenu() {
      return this.$route.path;
    },
    isAdmin() {
      const user = JSON.parse(localStorage.getItem("user"));
      return user && user.isLeader === 1;
    }
  },
  created() {
    this.loadUserInfo();
    // 从本地存储读取侧边栏状态（如果有）
    const sidebarState = localStorage.getItem('sidebarState');
    if (sidebarState !== null) {
      this.showSidebar = JSON.parse(sidebarState);
    }
  },
  methods: {
    loadUserInfo() {
      const user = JSON.parse(localStorage.getItem('user'));
      if (user && user.username) {
        this.username = user.username;
      }
    },
    handleCommand(command) {
      if (command === 'logout') {
        this.logout();
      } else if (command === 'profile') {
        this.$router.push('/user/MyProfile');
        // 进入个人中心时隐藏侧边栏
        this.showSidebar = false;
        this.saveSidebarState();
      }
    },
    logout() {
      this.$request.get('/exitLogin')
          .then(response => {
            if (response.code === 200) {
              this.$message.success('退出成功');
              localStorage.removeItem('user');
              this.$router.push('/');
            } else {
              this.$message.error(response.message || '退出失败，请重试');
            }
          })
          .catch(error => {
            console.error('退出登录请求失败:', error);
            this.$message.error('网络异常，请检查连接');
          });
    },
    // 侧边栏切换方法
    toggleSidebar() {
      this.showSidebar = !this.showSidebar;
      // 保存侧边栏状态到本地存储
      this.saveSidebarState();
    },
    saveSidebarState() {
      localStorage.setItem('sidebarState', JSON.stringify(this.showSidebar));
    }
  }
};
</script>

<style scoped>
.app-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  height: 60px !important;
  background-color: #409eff;
  color: white;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  align-items: center;
}

.sidebar-toggle {
  color: white;
  margin-right: 10px;
  padding: 0;
  font-size: 18px;
}

.logo {
  font-size: 18px;
  font-weight: 600;
  display: flex;
  align-items: center;
}

.sidebar {
  background-color: #2f3640;
  color: white;
  overflow: hidden; /* 确保侧边栏隐藏时内容不溢出 */
  transition: width 0.3s ease; /* 添加过渡动画 */
}

.main-content {
  padding: 20px;
  background-color: #f5f7fa;
  overflow-y: auto;
  transition: padding-left 0.3s ease; /* 添加过渡动画 */
}

/* 侧边栏过渡动画 */
.sidebar-transition-enter-active,
.sidebar-transition-leave-active {
  transition: width 0.3s ease;
}

.sidebar-transition-enter,
.sidebar-transition-leave-to {
  width: 0px !important;
}

/* 路由视图过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}
.fade-enter,
.fade-leave-to {
  opacity: 0;
}

/* 辅助类 */
.ml-1 {
  margin-left: 4px;
}
.ml-2 {
  margin-left: 8px;
}
.mr-1 {
  margin-right: 4px;
}
.mr-2 {
  margin-right: 8px;
}
.user-info {
  cursor: pointer;
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  color: white;
  font-size: 14px;
}

.el-dropdown-link:hover {
  opacity: 0.9;
}

.el-dropdown-link .el-avatar {
  background-color: #ffffff;
  color: #409eff;
  border: 1px solid #ddd;
}
</style>